#!/bin/bash

ThreadsForMake=4
Prefix=/my
BaseDir=/my/src

#===============================================================================

ScriptDir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
ColorRed='\e[1;31m'
ColorGreen='\e[0;32m'
ColorNone='\e[00m'

HandleStatus () {
  if [[ $1 != 0 ]]; then
    printf "\n${ColorRed}$CurrentLib FAILED!${ColorNone}\n"
    read -n 1 -p "Do you want to try again? (y/n)" doContinue
    if [[ ("$doContinue" != "y") && ("$doContinue" != "Y") ]]; then
      exit $1
    fi
  fi
}

#===============================================================================

if [[ "$GccArch" != "64" && "$GccArch" != "32" ]]; then echo "Wrong GccArch (${GccArch})!" ; exit 1 ; fi

InstallCmd="make install"
BuildDebug=false

for arg in "$@" ; do
  if [[ "$arg" == "noinstall" ]]; then
    InstallCmd=echo
  elif [[ "$arg" == "debug" ]]; then
    BuildDebug=true
  elif [[ "$arg" != "" ]]; then
    echo -e "\nUsage: ptBuildDeps [noinstall]"
    echo "This script does not build Qt."
    exit 0
  fi
done

#===============================================================================

printf "\nBuildDebug: %s\nInstallCmd: %s\n" "$BuildDebug" "$InstallCmd"
printf "\n$( set|grep FLAGS= )\n\n"

#===============================================================================

CurrentLib=LibPNG
printf "$ColorGreen========== $CurrentLib ==========$ColorNone\n"

ExitStatus=1
while [[ $ExitStatus != 0 ]]; do
  cd $BaseDir/libpng-* && \
  ./configure --prefix=$Prefix && \
  make -j$ThreadsForMake && \
  $InstallCmd
  ExitStatus=$?
  HandleStatus $ExitStatus
done



printf "\n\n"
read -n 1 -p "Build Qt now, then press any key." ptdummy
printf "\n\n"



CurrentLib=LibJPEG
printf "\n$ColorGreen========== $CurrentLib ==========$ColorNone\n"

ExitStatus=1
while [[ $ExitStatus != 0 ]]; do
  cd $BaseDir/jpeg-* && \
  ./configure --prefix=$Prefix && \
  cat jconfig.h | sed 's/#undef HAVE_PROTOTYPES/#define HAVE_PROTOTYPES 1/' >jconfig.h && \
  make -j$ThreadsForMake && \
  $InstallCmd
  HandleStatus $?


CurrentLib=LibTIFF
printf "\n$ColorGreen========== $CurrentLib ==========$ColorNone\n"

ExitStatus=1
while [[ $ExitStatus != 0 ]]; do
  cd $BaseDir/tiff-* && \
  ./configure --prefix=$Prefix && \
  make -j$ThreadsForMake && \
  $InstallCmd
  ExitStatus=$?
  HandleStatus $ExitStatus
done


CurrentLib=Lcms2
printf "\n$ColorGreen========== $CurrentLib ==========$ColorNone\n"

ExitStatus=1
while [[ $ExitStatus != 0 ]]; do
  cd $BaseDir/lcms2-* && \
  ./configure --prefix=$Prefix && \
  make -j$ThreadsForMake && \
  $InstallCmd
  ExitStatus=$?
  HandleStatus $ExitStatus
done


CurrentLib=LibIconv
printf "\n$ColorGreen========== $CurrentLib ==========$ColorNone\n"

ExitStatus=1
while [[ $ExitStatus != 0 ]]; do
  cd $BaseDir/libiconv-* && \
  patch -u -f windows/libiconv.rc $ScriptDir/ptBuildDeps.libiconv.rc.patch
  patch -u -f windows/iconv.rc $ScriptDir/ptBuildDeps.iconv.rc.patch
  ./configure --prefix=$Prefix && \
  make -j$ThreadsForMake && \
  $InstallCmd
  ExitStatus=$?
  HandleStatus $ExitStatus
done


CurrentLib=Exiv2
printf "\n$ColorGreen========== $CurrentLib ==========$ColorNone\n"

if [[ $BuildDebug == true ]]; then
  BuildType=debugfull
else
  BuildType=release
fi

ExitStatus=1
while [[ $ExitStatus != 0 ]]; do
  cd $BaseDir/exiv2-* && \
  mkdir -p build && \
  cd build && \
  cmake .. -G "MSYS Makefiles" -DCMAKE_INSTALL_PREFIX=$Prefix -DCMAKE_BUILD_TYPE=$BuildType && \
  make -j$ThreadsForMake && \
  $InstallCmd
  ExitStatus=$?
  HandleStatus $ExitStatus
done


CurrentLib=fftw
printf "\n$ColorGreen========== $CurrentLib ==========$ColorNone\n"

ExitStatus=1
while [[ $ExitStatus != 0 ]]; do
  cd $BaseDir/fftw-* && \
  ./configure --prefix=$Prefix --enable-shared --disable-static --with-combined-threads --enable-portable-binary --enable-sse2 --with-our-malloc16 && \
  make -j$ThreadsForMake && \
  $InstallCmd
  ExitStatus=$?
  HandleStatus $ExitStatus
done


CurrentLib=GraphicsMagick
printf "\n$ColorGreen========== $CurrentLib ==========$ColorNone\n"

if [[ $GccArch == 64 ]]; then
  BasetsdFile=/mingw/x86_64-w64-mingw32/include/basetsd.h
elif [[ $GccArch == 32 ]]; then
  BasetsdFile=/mingw/include/basetsd.h
fi
if [[ $BuildDebug == true ]]; then
  GMDebugOpts="--disable-openmp"
else
  GMDebugOpts=""
fi

ExitStatus=1
while [[ $ExitStatus != 0 ]]; do
  cd $BaseDir/GraphicsMagick-* && \
  cp $BasetsdFile basetsd.h.original && \
  patch -u $BasetsdFile $ScriptDir/ptBuildDeps.basetsd.h.${GccArch}.patch && \
  ./configure --prefix=$Prefix --enable-shared --with-quantum-depth=16 --disable-static --without-modules --without-perl --without-lcms $GMDebugOpts && \
  make -j$ThreadsForMake && \
  $InstallCmd &&\
  cp -r $Prefix/include/GraphicsMagick/* $Prefix/include
  ExitStatus=$?
  cp basetsd.h.original $BasetsdFile
  HandleStatus $ExitStatus
done


CurrentLib=LibLqr
printf "\n$ColorGreen========== $CurrentLib ==========$ColorNone\n"

ExitStatus=1
while [[ $ExitStatus != 0 ]]; do
  cd $BaseDir/liblqr-* && \
  ./configure --prefix=$Prefix && \
  make -j$ThreadsForMake && \
  $InstallCmd
  ExitStatus=$?
  HandleStatus $ExitStatus
done


printf "\n${ColorGreen}Finished successfully.$ColorNone\n"
